// utils/dateUtils.js

export const getFormattedDates = () => {
  const tabsList2 = ['今日', '昨日', '前天', '前天的前一天', '前天的前一天的前一天', '全部'];

  // 获取中间三个日期标签
  const getMiddleThreeDates = (array) => {
    if (array.length < 3) return [];
    const middleIndex = Math.floor(array.length / 2);
    return array.slice(middleIndex - 1, middleIndex + 2);
  };

  // 计算实际日期并转换为“月-日”格式
  const formatDate = (dateStr) => {
    const [year, month, day] = dateStr.split('-').map(Number);
    const date = new Date(year, month - 1, day);
    const formattedMonth = String(date.getMonth() + 1).padStart(2, '0');
    const formattedDay = String(date.getDate()).padStart(2, '0');
    return `${formattedMonth}-${formattedDay}`;
  };

  const calculateDate = (label) => {
    const today = new Date();
    let date;
    if (label === '今日') date = new Date(today);
    else if (label === '昨日') date = new Date(today.setDate(today.getDate() - 1));
    else if (label === '前天') date = new Date(today.setDate(today.getDate() - 2));
    else if (label === '前天的前一天') date = new Date(today.setDate(today.getDate() - 3));
    else if (label === '前天的前一天的前一天') date = new Date(today.setDate(today.getDate() - 4));
    return formatDate(date.toISOString().split('T')[0]);
  };

  // 获取中间三个日期及其对应的实际日期
  const middleLabels = getMiddleThreeDates(tabsList2);
  const middleDates = middleLabels.map(label => calculateDate(label));

  // 插入中间日期到原数组中的适当位置
  const insertDatesIntoArray = (array, dates) => {
    const middleIndex = Math.floor(array.length / 2);
    array.splice(middleIndex - 1, 3, ...dates);
  };

  insertDatesIntoArray(tabsList2, middleDates);

  return tabsList2;
};
